<?php

namespace App\Http\Requests\Backend\Admin;

use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\Rule;

class RoleRequest extends FormRequest
{
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        return true;
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        switch($this->method()) {
            /*
             * index
             */
            case 'GET': {
                if ($this->routeIs('role.index')) {
                    return [
                        'name' => 'max:100',
                    ];
                } else if ($this->routeIs('role.show')) {
                    return [

                    ];
                } else {
                    return [];
                }
            }
            /*
             * store
             */
            case 'POST': {
                return [
                    'name' => 'required|max:100|unique:admin_roles,name',
                    'identifier' => 'required|max:100|unique:admin_roles,identifier',
                    'number' => 'integer|min:1',
                ];
            }
            /*
             * update
             */
            case 'PUT':
            case 'PATCH': {
                $id = $this->route('id');
                return [
                    'name' => [
                        'required',
                        'max:100',
                        Rule::unique('admin_roles', 'name')->ignore($id)
                    ],
                    'identifier' => [
                        'required',
                        'max:100',
                        Rule::unique('admin_roles', 'identifier')->ignore($id)
                    ],
                    'number' => 'integer|min:1',
                ];
            }
            default: {
                return [];
            }
        }
    }

    /**
     * 验证信息返回
     *
     * @return array
     */
    public function messages()
    {
        return [
            'name.required' => '缺少角色名称',
            'name.max' => '角色名称超出范围',
            'name.unique' => '角色名称重复',
            'identifier.required' => '缺少角色编号',
            'identifier.max' => '角色编号超出范围',
            'identifier.unique' => '角色编号重复',
            'number.integer' => '数量限制类型超出范围',
            'number.min' => '数量限制类型超出范围',
        ];
    }
}
